<template>
<div style="background-color: white">
  <el-form :inline="true"  class="demo-form-inline">

    <el-form-item label="分组名称" prop="region">
      <el-select v-model="parentId"  placeholder="请选择分类名称" @change="getClassify(parentId)">
        <el-option v-for="item in group" :label="item.value" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-button type="primary" icon="el-icon-plus"  @click="isAddGroup=true">新增分类</el-button>
    <el-button type="success" icon="el-icon-plus"  @click="isAddClassify=true">添加分类内容</el-button>

    <el-dialog title="新增分类" :visible.sync="isAddGroup" center>
      <el-form-item label="添加" style="margin-left: 30px">
        <el-input
            placeholder="请输入内容"
            v-model="classify.value"
            clearable>
        </el-input>
      </el-form-item>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isAddGroup = false">取 消</el-button>
        <el-button type="primary" @click="addGroup()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加分类内容" :visible.sync="isAddClassify" center>
      <el-form-item label="分类名称" prop="region">
        <el-select v-model="parentId"  placeholder="请选择分类名称" @change="getClassify">
          <el-option v-for="item in group" :label="item.value" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
            placeholder="请输入内容"
            v-model="classify.value"
            clearable>
        </el-input>
      </el-form-item>

      <div slot="footer" class="dialog-footer">
        <el-button @click="isAddClassify = false">取 消</el-button>
        <el-button type="primary" @click="isAddClassify = addClassify()">确 定</el-button>
      </div>
    </el-dialog>


    <el-table
        v-loading="loading"
        :data="pageInfo.list"
        style="width: 1115px"
        :stripe=true
        :border=true
        height="500">
      <el-table-column
          prop="value"
          label="分类名称"
          width="350">
      </el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="scope">
          <el-button
              type="success"
              size="mini"
              @click="">编辑
          </el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="屏蔽/开放">-->
<!--        <template slot-scope="scope">-->
<!--          <el-switch-->
<!--              @change="deleteNews($event,scope.row.newsId)"-->
<!--              v-model="scope.row.isDelete"-->
<!--              :inactive-value="1"-->
<!--              :active-value="0"-->
<!--              active-color="#13ce66"-->
<!--              inactive-color="#ff4949">-->
<!--          </el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
  </el-form>
</div>
</template>

<script>
export default {
  name: "classify",
  data(){
    return{
      isAddGroup:false,
      isAddClassify:false,
      loading:false,
      parentId:'',
      group:[],
      pageInfo:{},
      classify: {
        value:'',
        parentId:''
      },//新增的分类名称
    }
  },
  methods:{
    getGroupName(){
      this.loading=true;
      this.$ajax.post(this.$config.url+"/ljblog/classify/getGroupName").then((res)=>{
        this.group=res.data;
        this.loading=false;
      })
    },
    getClassify(groupName){
      if(groupName!==null&&groupName!==undefined){
        this.$ajax.post(this.$config.url+"/ljblog/classify/getClassifyByPid",{
          "parentId":groupName
        }).then(res=>{
          this.pageInfo=res.data;
        })
      }
    },
    addClassify(){
      if(this.parentId!==null&&this.parentId!==undefined){
        this.classify.parentId=this.parentId
        this.$ajax.post(this.$config.url+"/ljblog/classify/addClassify",this.classify).then(res=>{
          console.log(res);
          if(res.data.status===this.$config.request_code_success){
            this.$message.success(res.data.message)
          }else{
            this.$message.error(res.data.message)
          }
        })
      }
    },
    addGroup(){
      this.classify.parentId=-1;
      this.$ajax.post(this.$config.url+"/ljblog/classify/addClassify",this.classify).then(res=>{
        console.log(res);
        if(res.data.status===this.$config.request_code_success){
          this.$message.success(res.data.message)
        }else{
          this.$message.error(res.data.message)
        }
      })
    }
  },
  mounted() {
    this.getGroupName();
  }

}
</script>

<style scoped>

</style>